import { login, logout, getInfo, doGet, doPost } from '@/api/index'
import { getToken, setToken, removeToken } from '@/utils/auth'
const user = {
  state: {
    user: '',
    status: '',
    post: '',
    token: getToken(),
    name: '',
    villageID: '',
    nickName: '',
    phone: '',
    avatar: '',
    roles: [],
    perms: -1,
    realName: '',
    village: '',
    outUnit: '',
    iconUrl: '',
    fistFlag: '',
    userForm: {}
  },

  mutations: {
    SET_USER: (state, user) => {
      state.user = user
    },
    SET_STATUS: (state, status) => {
      state.status = status
    },
    SET_POST: (state, post) => {
      state.post = post
    },
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_PHONE: (state, phone) => {
      state.phone = phone
    },
    SET_VILLAGEID: (state, villageID) => {
      state.villageID = villageID
    },
    SET_NICK: (state, nickName) => {
      state.nickName = nickName
    },
    SET_PERMS: (state, perms) => {
      state.perms = perms
    },
    SET_REAL: (state, realName) => {
      state.realName = realName
    },
    SET_VILLAGE: (state, village) => {
      state.village = village
    },
    SET_OUTUNIT: (state, outUnit) => {
      state.outUnit = outUnit
    },
    SET_ICONURL: (state, iconUrl) => {
      state.iconUrl = iconUrl
    },
    SET_FLAG: (state, fistFlag) => {
      state.fistFlag = fistFlag
    },
    SET_USERFORM: (state, userForm) => {
      state.userForm = userForm
    }
  },

  actions: {
    // 登录
    Login({commit}, userInfo) {
      return new Promise((resolve, reject) => {
        login(userInfo).then(response => {
          const data = response.data
          if (response.code == 200) {
            // commit('SET_TOKEN', data.token)
            sessionStorage.setItem('SET_TOKEN', data.token)
            setToken(data.token)
            resolve(response)
          } else {
            reject(response)
          }
        }).catch(error => {
          reject(error)
        })
      })
    },
    GetInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getInfo().then(response => {
          const data = response.data.user
          commit('SET_USERFORM', data)
          commit('SET_PERMS', data.userType)
          commit('SET_STATUS', response.status)
          commit('SET_NAME', data.userName)
          commit('SET_NICK', data.nickName)
          commit('SET_REAL', data.realName)
          commit('SET_POST', data.post)
          commit('SET_VILLAGEID', data.villageID)
          commit('SET_PHONE', data.phone)
          commit('SET_VILLAGE', data.village)
          commit('SET_OUTUNIT', data.outUnit)
          commit('SET_FLAG', response.data.forceChgPwd)
          commit('SET_ICONURL', data.headUrl)
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 动态修改权限
    ChangeRoles({ commit, dispatch }, role) {
      return new Promise(resolve => {
        const data = state
        commit('SET_TOKEN', data.token)
        commit('SET_ROLES', data.roles)
        commit('SET_PERMS', data.perms)
        commit('SET_NAME', data.name)
        commit('SET_AVATAR', data.avatar)
        dispatch('GenerateRoutes', data)
        setToken(role)
      })
    },
    LogOut({commit}) {
      return new Promise(resolve => {
        logout().then(() => {
          // commit('SET_TOKEN', '')
          sessionStorage.setItem('SET_TOKEN', '')
          // commit('SET_ROLES', [])
          commit('CLEAR_LOCK')
          removeToken()
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    }
  }

}

export default user


